/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "WMOFile.h"

namespace PQGameCore
{
	WMOFile::WMOFile(PQEngine::FileBase* f)
	{
		mpq_file=f;
		version=0;
		if(f!=0){
			init(f);
		}
	}

	void WMOFile::init(PQEngine::FileBase* f)
	{
		mpq_file=f;

		f->open();
		char fourcc[5];
		int size=0;
		while(!f->eof()){
			f->read(fourcc,4);
			f->read(&size, 4);
			flipcc(fourcc);
			fourcc[4] = 0;

			if (size == 0){
				continue;
			}

			int nextpos = f->getPosition() + size;
			if (strcmp(fourcc,"MVER")==0) {
				f->read(&version,4);
			}else if (strcmp(fourcc,"MOHD")==0) {
				initMOHD(f,size);
			}else if(strcmp(fourcc,"MOTX")==0){
				initMOTX(f,size);
			}else if(strcmp(fourcc,"MOMT")==0){
				initMOMT(f,size);
			}else if(strcmp(fourcc,"MOGN")==0){
				initMOGN(f,size);
			}else if(strcmp(fourcc,"MOGI")==0){
				initMOGI(f,size);
			}else if(strcmp(fourcc,"MOSB")==0){
				initMOSB(f,size);
			}else if(strcmp(fourcc,"MOPV")==0){
				initMOPV(f,size);
			}else if(strcmp(fourcc,"MOPT")==0){
				initMOPT(f,size);
			}else if(strcmp(fourcc,"MOPR")==0){
				initMOPR(f,size);
			}else if(strcmp(fourcc,"MOVV")==0){
				initMOVV(f,size);
			}else if(strcmp(fourcc,"MOVB")==0){
				initMOVB(f,size);
			}else if(strcmp(fourcc,"MOLT")==0){
				initMOLT(f,size);
			}else if(strcmp(fourcc,"MODS")==0){
				initMODS(f,size);
			}else if(strcmp(fourcc,"MODN")==0){
				initMODN(f,size);
			}else if(strcmp(fourcc,"MODD")==0){
				initMODD(f,size);
			}else if(strcmp(fourcc,"MFOG")==0){
				initMFOG(f,size);
			}else if(strcmp(fourcc,"MCVP")==0){
				initMCVP(f,size);
			}
			f->seek(nextpos);
		}	

		f->close();
		
		
		groups=new WMOGroupFile*[mohd->nGroups];
		for (int i=0; i<mohd->nGroups; i++){
			char temp[256];
			strcpy(temp, f->getFileName().c_str());
			int len=strlen(temp);
			temp[len-4] = 0;
			
			char fname[256];
			sprintf(fname,"%s_%03d.wmo",temp, i);
			
			assert(!"Bugs here");
			//MPQFile *f=SWOWArchive.getM.getMpqFile(fname);
			//groups[i]=new WMOGroupFile(this,f,i,mogis+i,mogn);
		}

	}

	void WMOFile::initMOHD(PQEngine::FileBase *f,int size)
	{
		assert(size==sizeof(wmo_mohd));
		mohd=new wmo_mohd;
		f->read(mohd,size);	
	}

	void WMOFile::initMOTX(PQEngine::FileBase *f,int size)
	{
		motx = new char[size+1];
		f->read(motx,size);
		motx[size] = 0;
	}

	void WMOFile::initMOMT(PQEngine::FileBase *f,int size)
	{
		momtCount=mohd->nMaterials;
		assert(size==(sizeof(wmo_momt)*momtCount));
		
		momts=new wmo_momt[momtCount];
		f->read(momts,size);
	}

	void WMOFile::initMOGN(PQEngine::FileBase *f,int size)
	{
		mogn=new char[size+1];
		f->read(mogn,size);
		mogn[size]=0;

		/*
		char* p=mognbuf;
		mognCount=0;
		while (p<mognbuf+size) {
			p+=strlen(p)+1;
			if(strlen(p)!=0){			
				mognCount++;
			}
		}

		mogns=new char*[mognCount];
		p=mognbuf;
		int i=0;
		while (p<mognbuf+size) {
			if(strlen(p)!=0){		
				mogns[i++]=p;
			}
			p+=strlen(p)+1;
		}
		*/

	}

	void WMOFile::initMOGI(PQEngine::FileBase *f,int size)
	{
		int cnt=mohd->nGroups;
		assert(size==(sizeof(wmo_mogi)*cnt));

		mogis=new wmo_mogi[cnt];
		f->read(mogis,size);
	}

	void WMOFile::initMOSB(PQEngine::FileBase *f,int size)
	{
		//Always 00 00 00 00
		//Skyboxes are now defined in DBCs (Light.dbc etc.). 
		//Contained a M2 filename that was used as skybox.
	}

	void WMOFile::initMOPV(PQEngine::FileBase *f,int size)
	{
		pvsize=size;
		int cnt=mohd->nPortals;	
		assert(size==(4*3*sizeof(float)*cnt));
		mopv=new float[4*3*cnt];// 4 * 3 * float per portal,cnt portals
		
		f->read(mopv,size);
	}


	void WMOFile::initMOPT(PQEngine::FileBase *f,int size)
	{
		/*
		Portal information. 20 bytes per portal, nPortals entries.
		Offset	Type 		Description
		0x00 	uint16 		Base vertex index?
		0x02 	uint16 		Number of vertices (?), always 4 (?)
		0x04 	3*float 	a normal vector maybe? haven't checked.
		0x10 	float 		unknown  - if this is NAN, the three floats will be (0,0,1)
		*/
	}

	void WMOFile::initMOPR(PQEngine::FileBase *f,int size)
	{
		prsize=size;
		int cnt=(int)size/sizeof(wmo_mopr);
		moprs=new wmo_mopr[cnt];
		f->read(moprs,size);
	}


	void WMOFile::initMOVV(PQEngine::FileBase *f,int size)
	{
		/*
		Visible block vertices
		Just a list of vertices that corresponds to the visible block list.
		*/
	}

	void WMOFile::initMOVB(PQEngine::FileBase *f,int size)
	{
		/*
		Visible block list
		unsigned short firstVertex;
		unsigned short count;
		*/
	}

	void WMOFile::initMOLT(PQEngine::FileBase *f,int size)
	{
		int cnt=mohd->nLights;

		assert(size==(sizeof(wmo_molt)*cnt));

		molts=new wmo_molt[cnt];
		f->read(molts,size);

	}

	void WMOFile::initMODS(PQEngine::FileBase *f,int size)
	{
		int cnt=mohd->nDoodadSets;

		assert(size==(sizeof(wmo_mods)*cnt));

		modss=new wmo_mods[cnt];
		f->read(modss,size);
	}

	void WMOFile::initMODN(PQEngine::FileBase *f,int size)
	{
		char* modns=new char[size];
		f->read(modns,size);
	}

	void WMOFile::initMODD(PQEngine::FileBase *f,int size)
	{
		ddsize=size;
		ddcnt=(int)size/sizeof(wmo_modd);
		modds=new wmo_modd[ddcnt];
		f->read(modds,size);
	}

	void WMOFile::initMFOG(PQEngine::FileBase *f,int size)
	{
		fogcnt=(int)size/sizeof(wmo_mfog);
		mfogs=new wmo_mfog[fogcnt];
		f->read(mfogs,size);
	}

	void WMOFile::initMCVP(PQEngine::FileBase *f,int size)
	{
		/*
		MCVP chunk (optional)
		Convex Volume Planes. Contains blocks of floating-point numbers.
		*/
	}

}
